//收藏
function collect(id,me) {
    var user=JSON.parse(sessionStorage.getItem("user"));
    $.ajax({
        url:"easybuy/changeCollect",
        type:"post",
        data:{
            id:id,
            userId:user.id,
        },
        success:function (data){
            if(data=="true"){
                $("#MyDiv_show").html("您已成功收藏该商品");
                ShowDiv('MyDiv1','fade1');
                me.attr('onclick','afterCollect()');
            }else {
                // $("#MyDiv_show").html("您已成功取消收藏");
                // ShowDiv('MyDiv1','fade1');
            }
        }
    });
}
//收藏后
function afterCollect() {
    $("#MyDiv_show").html("您已过收藏该商品，请勿重复操作");
    ShowDiv('MyDiv1','fade1');
}
//删除
function dele1(id){
    $(".b_sure").attr('onclick','dele2('+id+')');
    ShowDiv('MyDiv','fade');
}
function dele2(id){
    var user=JSON.parse(sessionStorage.getItem("user"));
    $.ajax({
        url:"easybuy/deleWares",
        data:{
            id:id,
            userId:user.id
        },
        success:function (data) {
            sum(user.id);
            $("[waresId='"+id+"']").remove();
            CloseDiv('MyDiv','fade');
            if ($("#tr1").parent().children().length==3){
                $("#tr1").after("<tr><td colspan='6' style='font-size: 3em;text-align: center;'>你还没有添加任何商品</td></tr>");
                $("[name='ll']").remove();
            }
        }
    })
}
//总价
function sum(userId) {
    var total=shopping(userId);
    $("#total").html("￥"+total);
}
//增加
function add(a){
    addUpdate1(a);
    a.siblings("[name='num']").change();
}
//减少
function jian(a){
    jianUpdate1(a);
    a.siblings("[name='num']").change();
}
$(function () {
    var user=JSON.parse(sessionStorage.getItem("user"));

    $.ajax({
        url:"easybuy/shopping",
        dataType:"json",
        type: "post",
        asycn:false,
        data:{
            userId:user.id
        },
        success:function (shopList) {
            if(shopList==null||shopList==''){
                $("#tr1").after("<tr><td colspan='6' style='font-size: 3em;text-align: center'>你还没有添加任何商品</td></tr>");
            }else {
                $("#tr1").nextAll().remove();
                var total=0;
                for (var i in shopList){
                    total=total+shopList[i].cost;
                    var background="";
                    if (i%2==1){
                        background="class='car_tr'";
                    }
                    $("#tr1").parent().append("<tr "+background+" waresId='"+shopList[i].id+"'>\n" +
                        "            <td>\n" +
                        "            \t<div class=\"c_s_img\"><img src='images/"+shopList[i].fileName+"' width=\"73\" height=\"73\" /></div>\n" +
                        "                "+shopList[i].name+"\n" +
                        "            </td>\n" +
                        "            <td name='price' align=\"center\" style=\"color:#ff4e00;\">￥"+shopList[i].price+"</td>\n" +
                        "            <td align=\"center\">\n" +
                        "            \t<div class=\"c_num\">\n" +
                        "                    <input type=\"button\" value=\"\" onclick=\"jian($(this))\" class=\"car_btn_1\" />\n" +
                        "                \t<input max='"+shopList[i].stock+"' name='num' type=\"text\" value='"+shopList[i].quantity+"' name=\"\" class=\"car_ipt\" />  \n" +
                        "                    <input type=\"button\" value=\"\" onclick=\"add($(this))\" class=\"car_btn_2\" />\n" +
                        "                </div>\n" +
                        "            </td>\n" +
                        "            <td name='cost' align=\"center\" style=\"color:#ff4e00;\">￥"+shopList[i].cost+"</td>\n" +
                        "            <td align=\"center\"><a onclick='dele1("+shopList[i].id+")'>删除</a>&nbsp; &nbsp;<a onclick='collect("+shopList[i].id+",$(this))'>加入收藏</a></td>\n" +
                        "          </tr>")
                }
                $("#tr1").parent().append("<tr height=\"70\">\n" +
                    "          \t<td name='ll' colspan=\"6\" style=\"font-family:'Microsoft YaHei'; border-bottom:0;\">\n" +
                    "                <span class=\"fr\">商品总价：<b id='total' style=\"font-size:22px; color:#ff4e00;\">￥"+total+"</b></span>\n" +
                    "            </td>\n" +
                    "          </tr>\n" +
                    "          <tr name='ll' valign=\"top\" height=\"150\">\n" +
                    "          \t<td colspan=\"6\" align=\"right\">\n" +
                    "            \t<a href='Index.html'><img src=\"images/buy1.gif\" /></a>&nbsp; &nbsp; <a href='BuyCar_Two.html'><img src=\"images/buy2.gif\" /></a>\n" +
                    "            </td>\n" +
                    "          </tr>")
            }
            $("[name='num']").change(function () {
                var price=$(this).parents("td").siblings("[name='price']").html();
                price=price.slice(1);
                var num=parseInt($(this).val());
                var max=parseInt($(this).attr('max'));
                if (!isNaN(num)){
                    $(this).val(num);
                } else {
                    $(this).val(1);
                }
                if (parseInt($(this).val())>max){
                    $(this).val(max);
                }else if (parseInt($(this).val())<1){
                    $(this).val(1);
                }
                $(this).parents("td").siblings("[name='cost']").html("￥"+parseInt(parseInt($(this).val())*price));
                var id=$(this).parents("tr").attr("waresId");
                var quantity=$(this).val();
                $.ajax({
                    url:"easybuy/modifyWares",
                    data:{
                        id:id,
                        userId:user.id,
                        quantity:quantity
                    },
                    success:function (data) {
                        sum(user.id);
                    }
                });
            });
        }
    });

})